//index.js
import util from '../../utils/util';

//获取应用实例
Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    searchList: [],
    lawList: [],
    categorylawList: []
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  bindLawInput: function(_event) {
    this.setData({
      searchContent: _event.detail.value
    });
  },
  bindSearchLaw: function(_event) {
    if(!!_event.target.dataset.search){
      wx.navigateTo({
        url: '../search/index?search='+ _event.target.dataset.search
      })
      return;
    }
    if(!!this.data.searchContent){
      util.getOrSetHistorySearch(this.data.searchContent);
      wx.navigateTo({
        url: '../search/index?search='+ this.data.searchContent
      })
    }
  },
  onLoad: function () {
    var that = this;
    
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    });

    var _appInstance = getApp();
    var _globalData = _appInstance.globalData;
    this.setData({
      switchForVerify: !!_globalData.switchForVerify || false
    });

    wx.request({
      url: `${getApp().globalData.serverHost}/wx/get/verify`, //仅为示例，并非真实的接口地址
      method: "post",
      header: { 'content-type': 'application/json'},
      success: function(res) {
        var _verify = res.data.switchForVerify || false;
        _globalData.switchForVerify = _verify;
        that.setData({
          switchForVerify: _verify
        });
      },
      fail: function(res){
        console.log(res);
      }
    });


    // 获取列表
    wx.request({
      url: `${getApp().globalData.serverHost}/wx/bd/type/categories`, //仅为示例，并非真实的接口地址
      data: {
        type: 10
      },
      method: "post",
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        console.log('11111: ', res);
        var _data = res.data || [];
        for(var i=0; i<_data.length; i++){
          _data[i].category.name = _data[i].category.name.replace(/\)$/, '条)');
          for(var j=0; j<_data[i].detail.length; j++){
              _data[i].detail[j].publishTime = util.formatCommonShortTime(_data[i].detail[j].publishTime);
              _data[i].detail[j].effectiveTime = util.formatCommonShortTime(_data[i].detail[j].effectiveTime);
          }
        }

        // for(var i=0; i<res.data.detail.length; i++){
        //     res.data.detail[i].publishTime = util.formatCommonShortTime(res.data.detail[i].publishTime);
        //     res.data.detail[i].effectiveTime = util.formatCommonShortTime(res.data.detail[i].effectiveTime);
        // }

        wx.hideToast();
        var _lawList = [];
        for(var i=0; i<res.data.length; i++){
          _lawList = _lawList.concat(res.data[i].detail);
        }
        that.setData({
          categorylawList: res.data,
          lawList: _lawList,
          count: res.data.count
        })
      },
      fail: function(res){
        console.log(res);
      }
    });


    // 获取列表
    wx.request({
      url: `${getApp().globalData.serverHost}/wx/bd/law/count`,
      data: {
        type: 10
      },
      method: "post",
      header: {
          'content-type': 'application/json'
      },
      success: function(res) {
        that.setData({
          count: res.data.count
        })
      },
      fail: function(res){
        console.log(res);
      }
    });

  },
  onShow:function(){
    // 页面显示
    // 获取历史记录记录
    var _history = util.getOrSetHistorySearch() || "";
    if(!!_history){
      this.setData({
        searchList: _history.split(",")
      })
    }
  },
  onShareAppMessage: function () {
    return {
      title: "法条大全",
      path: 'pages/index/index'
    }
  }
})
